package com.ruoyi.web.controller.system;

import java.lang.reflect.MalformedParameterizedTypeException;
import java.util.*;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ObjectName;
import com.ruoyi.system.service.IObjectNameService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.domain.Ztree;
import org.springframework.web.multipart.MultipartFile;

/**
 * 过程Controller
 * 
 * @author ruoyi
 * @date 2020-02-21
 */
@Controller
@RequestMapping("/system/object")
public class ObjectNameController extends BaseController {
    private String prefix = "system/object";

    @Autowired
    private IObjectNameService objectNameService;

    @RequiresPermissions("system:object:view")
    @GetMapping()
    public String object() {
        return prefix + "/object";
    }

    /**
     * 查询过程树列表
     */
    @RequiresPermissions("system:object:list")
    @PostMapping("/list")
    @ResponseBody
    public List<ObjectName> list(ObjectName objectName) {
        List<ObjectName> list = objectNameService.selectObjectNameList(objectName);
        return list;
    }

    /**
     * 导出过程列表
     */
    @RequiresPermissions("system:object:export")
    @Log(title = "过程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ObjectName objectName) {
        List<ObjectName> list = objectNameService.selectObjectNameList(objectName);
        ExcelUtil<ObjectName> util = new ExcelUtil<ObjectName>(ObjectName.class);
        return util.exportExcel(list, "object");
    }

    /**
     * 新增过程
     */
    @GetMapping(value = {"/add/{objectId}", "/add/"})
    public String add(@PathVariable(value = "objectId", required = false) Long objectId, ModelMap mmap) {
        if (StringUtils.isNotNull(objectId)) {
            mmap.put("objectName", objectNameService.selectObjectNameById(objectId));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存过程
     */
    @RequiresPermissions("system:object:add")
    @Log(title = "过程", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ObjectName objectName) {
        return toAjax(objectNameService.insertObjectName(objectName));
    }

    /**
     * 修改过程
     */
    @GetMapping("/edit/{objectId}")
    public String edit(@PathVariable("objectId") Long objectId, ModelMap mmap) {
        ObjectName objectName = objectNameService.selectObjectNameById(objectId);
        mmap.put("objectName", objectName);
        return prefix + "/edit";
    }


    /**
     * 修改保存过程
     */
    @RequiresPermissions("system:object:edit")
    @Log(title = "过程", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ObjectName objectName) {
        return toAjax(objectNameService.updateObjectName(objectName));
    }

    /**
     * 删除
     */
    @RequiresPermissions("system:object:remove")
    @Log(title = "过程", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{objectId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("objectId") Long objectId) {
        return toAjax(objectNameService.deleteObjectNameById(objectId));
    }

    /**
     * 选择过程树
     */
    @GetMapping(value = {"/selectObjectTree/{objectId}", "/selectObjectTree/"})
    public String selectObjectTree(@PathVariable(value = "objectId", required = false) Long objectId, ModelMap mmap) {
        if (StringUtils.isNotNull(objectId)) {
            mmap.put("objectName", objectNameService.selectObjectNameById(objectId));
        }
        return prefix + "/tree";
    }

    /**
     * 加载过程树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData() {
        List<Ztree> ztrees = objectNameService.selectObjectNameTree();
        return ztrees;
    }

    @GetMapping("/list/{objectId}")
    @ResponseBody
    public List<ObjectName> getParents(@PathVariable("objectId") Long objectId) {
        List<ObjectName> objetcNameParentByObectId = objectNameService.getObjetcNameParentByObectId(objectId);
        for (ObjectName objectName : objetcNameParentByObectId) {
            System.out.println(objectName);
        }
        return objetcNameParentByObectId;
    }


    @GetMapping("/detail/{objectId}")
    public String detail(@PathVariable("objectId") Long objectId, ModelMap mmap) {
        List<ObjectName> objetcNameParentByObectId = objectNameService.getObjetcNameParentByObectId(objectId);
        ObjectName objectName = objectNameService.selectObjectNameById(objectId);
        mmap.put("objectName", objectName);
        mmap.put("object", objetcNameParentByObectId);
        return prefix + "/detail";
    }


    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<ObjectName> util = new ExcelUtil<ObjectName>(ObjectName.class);
        return util.importTemplateExcel("接触点数据");
    }


    @PostMapping("/importData")
    @ResponseBody
    public void importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        /*AjaxResult
         * */
        ExcelUtil<ObjectName> util = new ExcelUtil<ObjectName>(ObjectName.class);
        List<ObjectName> ObjectList = util.importExcel(file.getInputStream());

        for (ObjectName Object : ObjectList){
            String deiflag = UUID.randomUUID().toString();
            Object.setDeiFlag(deiflag);
            this.addSave(Object);
        }

        Map<String, Long> FlagIds = new HashMap<>();
        List<ObjectName>  objectNames = objectNameService.getObjetcName();
        for (ObjectName objectName : objectNames){
            for (ObjectName Object : ObjectList){
                if (objectName.getObjectName().equals(Object.getParentName())) {
                    FlagIds.put(Object.getDeiFlag(),objectName.getObjectId());
                }
            }
        }
        for (Map.Entry<String,Long> FlagId : FlagIds.entrySet()){
            for (ObjectName objects : objectNames) {
                if (objects.getDeiFlag().equals(FlagId.getKey())) {
                    objects.setParentId(FlagId.getValue());
                    objectNameService.updateObjectName(objects);
                }
            }

        }


    }
}
